//文章管理
const express = require('express')
const router = express.Router()
const db = require('../utils/db')
const multer = require('multer')
const moment = require('moment')

const upload = multer({ dest: 'uploads/' })



//发布新文章
router.post('/my/article/add', upload.single('cover_img'), (req, res) => {
    const { id } = req.user
    console.log(id)
    const { title, cate_id, content, state } = req.body
    const { filename } = req.file
        //获取时间
    let time = moment().format('YYYY-MM-DD HH:mm:ss')
    db(`INSERT INTO article SET title='${title}',content='${content}',cover_img='${filename}',cate_id=${cate_id},state='${state}',is_delete=0,author_id=${id},pub_date='${time}'`, (err, result) => {
        if (err || result.affectedRows === 0) return res.send({ status: 1, message: '发布文章失败！' })
        res.send({ status: 0, message: '发布文章成功！' })
    })
})


// 获取文章的列表数据
router.get('/my/article/list', (req, res) => {
    let { pagenum, pagesize, cate_id, state } = req.query
    if (!cate_id) cate_id = '.+'
    if (!state) state = '.+';
    db(`SELECT article.id,title,pub_date,state,cate_name FROM article LEFT JOIN category ON category.id = article.cate_id WHERE cate_id REGEXP '^${cate_id}$' AND state REGEXP '^${state}$' ORDER BY id DESC LIMIT ${pagenum * pagesize - pagesize},${pagesize}`, (err, result) => {
        if (err || result.length === 0) return res.send({ status: 1, message: '获取文章列表失败！' })
        db(`SELECT COUNT(*) total FROM article`, (err, result1) => {
            const { total } = result1[0]
            let page = Math.ceil(total / pagesize)
            res.send({ status: 0, total: total, page: page, message: '获取文章列表成功！', data: result })
        })
    })
})


//根据 Id 删除文章数据
router.get('/my/article/delete/:id', (req, res) => {
    const { id } = req.params
    db(`UPDATE article SET is_delete =1 WHERE id=${id}`, (err, result) => {
        // console.log(result)
        if (err || result.changedRows === 0) return res.send({ status: 1, message: '删除失败，请检查参数' })
        res.send({ status: 0, message: '删除成功！' })
    })
})


// 根据 Id 获取文章详情
router.get('/my/article/:id', (req, res) => {
    const { id } = req.params
    db(`SELECT * FROM article WHERE id = ${id}`, (err, result) => {
        // console.log(result)
        if (err || result.length === 0) return res.send({ status: 1, message: '获取失败，请检查参数' })
        res.send({ status: 0, message: '获取文章成功！', data: result[0] })
    })
})



// 根据 Id 更新文章信息
router.post('/my/article/edit', upload.single('cover_img'), (req, res) => {
    const { id, title, cate_id, content, state } = req.body
    const { filename } = req.file
        //获取时间
    let time = moment().format('YYYY-MM-DD HH:mm:ss')
    db(`UPDATE article SET title='${title}',content='${content}',cover_img='${filename}',cate_id=${cate_id},state='${state}',pub_date='${time}' WHERE id = ${id}`, (err, result) => {
        console.log(result)
        if (err || result.affectedRows === 0) return res.send({ status: 1, message: '更新文章失败！' })
        res.send({ status: 0, message: '修改文章成功！' })
    })
})




module.exports = router